//
//  JYHouseLoanListViewController.swift
//  JYCalculate
//
//  Created by 何治军 on 2018/10/30.
//  Copyright © 2018 何治军. All rights reserved.
//

import UIKit


class JYHouseLoanListViewController: JYBaseViewController,UITableViewDataSource,UITableViewDelegate {
    
    /// 选择model后的回调
    var selectedModelBlock = {(model:JYHouseLoanModel) -> (Void) in}
    
    var currentSelectedModel:JYHouseLoanModel = JYHouseLoanModel()
    
    /// 数据
    var dataArray:[JYHouseLoanModel] = [] {
        didSet{
            self.tableview.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = Localized("currency-list");
        self.leftBtn.tintColor = UIColor.white
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.barTintColor = JYNavigationBarColor
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 19),NSAttributedString.Key.foregroundColor:UIColor.white]
    }
    
    override func setUpUI() {
        view.addSubview(tableview)
        tableview.register(JYCurrecnyListCell.self, forCellReuseIdentifier: "JYCurrecnyListCell")
        tableview.dataSource = self
        tableview.delegate = self
    }
    
    override func setDefaultInfo() {
        
    }
    
    override func setTheLayout() {
        tableview.mas_updateConstraints { (make) in
            make?.left.mas_equalTo()(self.view)
            make?.top.mas_equalTo()(self.view)
            make?.right.mas_equalTo()(self.view)
            make?.bottom.mas_equalTo()(self.view)
        }
    }
    
    //MARK: - UITableViewDataSource,UITableViewDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = JYHouseLoanListCell.init(style: .default, reuseIdentifier: "JYCurrecnyListCell")
        cell.model = self.dataArray[indexPath.row]
        if indexPath.row == 0 {
            cell.topLine.isHidden = false
        }
        if cell.model.descri == currentSelectedModel.descri {
            cell.cellIsSelected = true
        }else{
            cell.cellIsSelected = false
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableview.deselectRow(at: indexPath, animated: true)
        self.selectedModelBlock(self.dataArray[indexPath.row])
        self.navigationController?.popViewController(animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    // MARK: - 懒加载b
    private lazy var tableview:UITableView = {
        let tab = UITableView()
        tab.separatorStyle = .none
        tab.backgroundColor = JYVCBackGroundColor
        return tab
    }()
}

class JYHouseLoanListCell: JYBaseTableViewCell {
    
    var cellIsSelected:Bool = false {
        didSet{
            self.imgView.isHidden = !cellIsSelected
        }
    }
    
    var model:JYHouseLoanModel = JYHouseLoanModel(){
        didSet{
            self.firstLab.text = model.descri
            self.secondLab.text = model.defaultDescri
        }
    }
    
    override func setUpUI() {
        super.setUpUI()
        self.imgView.image = UIImage(named: "infolist_selected")
        self.contentView.addSubview(self.imgView)
        self.contentView.addSubview(self.firstLab)
        self.contentView.addSubview(self.secondLab)
    }
    
    override func setTheLayout() {
        super.setTheLayout()
        self.imgView.mas_updateConstraints { (make) in
            make?.left.mas_equalTo()(self.contentView)?.offset()(16)
            make?.width.mas_equalTo()(44)
            make?.height.mas_equalTo()(44)
            make?.centerY.mas_equalTo()(self.contentView)
        }
        self.firstLab.mas_updateConstraints { (make) in
            make?.left.mas_equalTo()(self.imgView.mas_right)?.offset()(16)
            make?.centerY.mas_equalTo()(self.contentView)
        }
        self.secondLab.mas_updateConstraints { (make) in
            make?.left.mas_equalTo()(self.firstLab.mas_right)?.offset()(16)
            make?.right.mas_equalTo()(self.contentView)
            make?.centerY.mas_equalTo()(self.contentView)
        }
        self.secondLab.setContentHuggingPriority(.fittingSizeLevel, for: .horizontal)
    }
    
    // MARK: - 懒加载
    
    /// 选择图标
    lazy var imgView:UIImageView = {
        let img = UIImageView()
        return img
    }()
    
    /// 第一个内容
    lazy var firstLab:UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 17)
        return lab
    }()
    
    /// 第二个内容
    lazy var secondLab:UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 17)
        return lab
    }()
}
